<?php

/* 管理分类的模型
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//处理提交的分类信息
function add_categorys($cate='',$cont=''){
     global $_DBLINK;
    $category = empty($cate) ? '' : $cate;
        $content = empty($cont) ? '' : $cont;

        $sql = 'INSERT INTO `categorys`(`title`,`description`) VALUES(\'' . $category . '\',\'' . $content . '\')';
        mysql_query($sql);
        //让p_id 自动增长1
        $p_id = mysql_insert_id();
        $sql1 = 'UPDATE `categorys` SET `p_id`=\'' . $p_id . '\' WHERE `id`=\'' . $p_id . '\'';
        mysql_query($sql1);

        if (mysql_affected_rows($_DBLINK) > 0) {
            $msg = '添加分类：\'' . $category . '\'成功';
            show_msg($msg, '/admin/categorys.php?action=default');
        } else {
            show_msg('添加分类失败', '/admin/categorys.php?action=default');
        }
}

//统计各个分类下的文章数量
function num_article($cid){
    $sql='SELECT COUNT(*) FROM `articles` WHERE `c_id`=\''.$cid.'\' GROUP BY `c_id` ';
    $result=mysql_query($sql);
    $rows=mysql_fetch_row($result);
    $nums=$rows[0];
    
    $sql='UPDATE `categorys` SET `atc_count`=\''.$nums.'\' WHERE `id`=\''.$cid.'\'';
    mysql_query($sql);
    
    
}

//处理修改的分类信息
function edit_categorys($cate='',$cont='',$cid=''){
    global $_DBLINK;
    $category = empty($cate) ? '' : $cate;
        $content = empty($cont) ? '' : $cont;

        $sql = 'UPDATE `categorys` SET `title`=\'' . $category . '\' ,`description`=\'' . $content . '\' WHERE `id`=\'' . $cid . '\'';
        mysql_query($sql);

        if (mysql_errno()) {
            echo 'error message:' . mysql_error();
        }
        if (mysql_affected_rows($_DBLINK) > 0) {
            $msg = '修改分类：\'' . $category .'+++'.$content. '\'成功';
            show_msg($msg, '/admin/categorys.php?action=default');
        } else {
            show_msg('分类没有修改', '/admin/categorys.php?action=default');
        }
}


//将分类的信息全部读出来
function mancate() {
    $result=mysql_query('SELECT `id` FROM `categorys`');
    while($rows=mysql_fetch_row($result)){
        $cid=$rows[0];
        num_article($cid);//统计各个分类下的文章数量；
    }
    
    
    $sql = "SELECT `id`,`title`,`atc_count` FROM `categorys` ORDER BY `id`";
    $result = mysql_query($sql);
    while ($rows = mysql_fetch_assoc($result)) {
        $cid = $rows['id'];
        
        $return.='<div class=\'rows\'>';
        foreach ($rows as $value) {
            $return.='<div class=\'cols\'>';
            $return.=$value;
            $return.='</div>';
        }
        $return.='<div class="cols">';
        $return.=manner($cid);
        $return.='</div>';
        $return.='</div>';
        $return.='<div class=\'ro\'></div>';
    }

    return $return;
}

function manner($cid) {
    $man = "<a href='categorys.php?action=edit&cid=" . $cid . "'>[修改]</a>&nbsp;";
    $man.="<a href='categorys.php?action=del&cid=" . $cid . "'>[删除]</a>";
    return $man;
}

//删除分类的方法
function del_cate($cid) {
    global $_DBLINK;
    $sql = 'DELETE FROM `categorys` WHERE `id`=' . $cid . '';
    mysql_query($sql);

    if (mysql_errno()) {
        echo 'cuowu :' . mysql_error();
    }
    if (mysql_affected_rows($_DBLINK) > 0) {
        show_msg('分类删除成功！', '/admin/categorys.php?action=default');
    } else {
        show_msg('分类删除失败！', '/admin/categorys.php?action=default');
    }
}

//将要修改的某个分类从数据库中读取出来
function show_cate($cid){
    $sql='SELECT `title`,`description` FROM `categorys` WHERE `id`='.$cid.' LIMIT 1';
    $result=mysql_query($sql);
    
    $rows=mysql_fetch_assoc($result);
    
    return $rows;    
}